import Vue from 'vue'
import Router from 'vue-router'
import routes from '@/router/Router-config'
Vue.use(Router)
import {
    Message,
} from 'element-ui'
const router = new Router({
    mode: "history",
    routes

})
router.beforeEach((to, from, next) => {
        if (to.path === '/login') {
            // localStorage.removeItem("token")
            next();
        } else {
            let token = localStorage.getItem('token');
            if (token == null || token === '') {
                Message({
                    message: "请先登录",
                    type: 'error',
                    duration: 2000
                })
                next("/login");
            } else {
                next();
            }


        }
    })
    //避免了到当前位置的冗余导航
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router